package cn.edu.zzuli.message.news.service;


import cn.edu.zzuli.common.constant.RabbitConstant;
import cn.edu.zzuli.common.utils.NowDate;
import cn.edu.zzuli.common.utils.WeChatUtil;
import cn.edu.zzuli.model.entity.WebMessage;
import cn.edu.zzuli.model.vo.wx.WxMessage;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MessageConsumerService {

    @Autowired
    private SendServiceImpl sendServiceImpl;

    @Autowired
    private WebSocket webSocket;

    /**
     * 接取订单监听
     * @param message
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(RabbitConstant.QUEUE_TAKE_ORDER),
                    exchange = @Exchange(value = RabbitConstant.EXCHANGE,type = "direct"),
                    key = {RabbitConstant.ROUTING_KEY_TAKE_ORDER}
            )
    })
    public void takeOrderMessage(String message){
        this.sendMessage(message);
    }

    /**
     * 取消订单监听
     * @param message
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(RabbitConstant.QUEUE_CANCEL_ORDER),
                    exchange = @Exchange(value = RabbitConstant.EXCHANGE,type = "direct"),
                    key = {RabbitConstant.ROUTING_KEY_CANCEL_ORDER}
            )
    })
    public void cancelOrderMessage(String message){
        this.sendMessage(message);
    }


    /**
     * 申请确认监听
     * @param message
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(RabbitConstant.QUEUE_APPLY_ORDER),
                    exchange = @Exchange(value = RabbitConstant.EXCHANGE,type = "direct"),
                    key = {RabbitConstant.ROUTING_KEY_APPLY_ORDER}
            )
    })
    public void applyOrderMessage(String message){
       this.sendMessage(message);
    }

    /**
     * 消息广播
     * @param message
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(RabbitConstant.QUEUE_BROADCAST_MESSAGES),
                    exchange = @Exchange(value = RabbitConstant.EXCHANGE,type = "direct"),
                    key = {RabbitConstant.ROUTING_KEY_BROADCAST_MESSAGES}
            )
    })
    public void broadcastMessages(String message){
        String[] split = message.split("[|]");
        WebMessage webMessage = new WebMessage();
        webMessage.setMessage(split[0]);
        webSocket.sendAll(webMessage,split[1]);
    }

    /**
     * 签到
     * @param message
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(RabbitConstant.QUEUE_SIGN_IN),
                    exchange = @Exchange(value = RabbitConstant.EXCHANGE,type = "direct"),
                    key = {RabbitConstant.ROUTING_KEY_SIGN_IN}
            )
    })
    public void SignIn(String message){
        JSONObject json = new JSONObject();
        json.set("time1", new JSONObject().set("value", NowDate.getNowTime()));
        json.set("thing3", new JSONObject().set("value", "今天还未签到"));
        WeChatUtil.wxSend(WxMessage.SIGN_IN,message,json);
    }

    /**
     * 消息
     * @param message
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(RabbitConstant.QUEUE_USER_CHAT),
                    exchange = @Exchange(value = RabbitConstant.EXCHANGE,type = "direct"),
                    key = {RabbitConstant.ROUTING_KEY_USER_CHAT}
            )
    })
    public void Chat(String message){
       sendServiceImpl.sendChatMessage(Integer.parseInt(message));
    }



    /**
     *  发送信息
     * @param message
     */
    private void sendMessage(String message){
        String[] strings = message.split("[|]");
        Integer id = Integer.parseInt(strings[0]);
        Integer type = Integer.parseInt(strings[1]);
        Integer uid = Integer.parseInt(strings[2]);
        log.info("id: {} type: {} uid: {}",id,type,uid);
        this.webSend(id,type,uid);
    }

    private void webSend(int id, int type, int uid) {
        Integer toUid = sendServiceImpl.getOfferUidByOid(id);
        log.info("toUid: {}",toUid);
        WebMessage message = new WebMessage();
        if(type == 1){
            message.setMessage("您有一个订单开始出发~~~");
            message.setUserNo(toUid);
            log.info("message : {}",message);
            webSocket.sendToUser(message,1,id,uid);
        }else if(type == 2){
            message.setMessage("您的订单被接单用户取消了 (っ °Д °)っ");
            message.setUserNo(toUid);
            log.info("message : {}",message);
            webSocket.sendToUser(message,2,toUid,uid);
        }else{
            message.setMessage("有一个订单正在等着您的核对（￣︶￣）↗");
            message.setUserNo(toUid);
            log.info("message : {}",message);
            webSocket.sendToUser(message,3,id,uid);
        }
    }


}
